package com.yamidragut.simpleebooksreader;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;
import android.content.ContextWrapper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class FilesSaver {
	
	Context appContext;
	
	public static final String LOG = "FilesSaver";
	
	public FilesSaver(Context appContext) {
		this.appContext = appContext;
	}
	
	
	/**
	 * Save the bitmap cover to a temporally file (when close the app the directory with all images is deleted)
	 * 
	 * @param bitmapImage
	 * @param directoryName
	 * @param fileName
	 * @return
	 */
	public String saveBitmapToInternalSorage(Bitmap bitmapImage, String directoryName, String fileName){
        ContextWrapper cw = new ContextWrapper(appContext);
        File directory = cw.getDir(directoryName, Context.MODE_PRIVATE);
        File coverFile = new File(directory,fileName);

        String coverPath = null;
        FileOutputStream fos = null;
        
        try {
            fos = new FileOutputStream(coverFile);

            // Use the compress method on the BitMap object to write image to the OutputStream
            bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.close();
            
            coverPath = coverFile.getAbsolutePath();
            
        } catch (FileNotFoundException e) {
            Log.e(LOG, "File not found in saveToInternalSorage: " + e.getMessage());
        } catch (IOException e) {
            Log.e(LOG, "Error accessing file in saveToInternalSorage: " + e.getMessage()); 
        } catch (Exception e) {
        	Log.e(LOG, "Error in saveToInternalSorage: " + e.getMessage());
        }
        
        
        return coverPath;
    }
	
	
	/**
	 * Method to get the bitmap of the cover
	 * @param path
	 */
	public static Bitmap loadBitmapCoverFromStorage(String coverPath) {

		Bitmap cover = null;
		
	    try {
	        File coverFile = new File(coverPath);
	        cover = BitmapFactory.decodeStream(new FileInputStream(coverFile));       
	    } catch (FileNotFoundException e) {
	    	Log.e(LOG, "File not found in loadBitmapCoverFromStorage: " + e.getMessage());
	    } catch (Exception e) {
	    	Log.e(LOG, "Error in loadBitmapCoverFromStorage: " + e.getMessage());
		}

	    return cover;
	}
	
	
	/**
	 * Method to get the absolute path to the image if it is already stored
	 * @param path
	 */
	public String getPathIfAlreadyStored(String directoryName, String fileName) {
		
        ContextWrapper cw = new ContextWrapper(appContext);
        File directory = cw.getDir(directoryName, Context.MODE_PRIVATE);
        
		String coverPath = null;
		
	    try {
	        File coverFile = new File(directory, fileName);
	        
	        if(coverFile.exists()) {
	        	coverPath = coverFile.getAbsolutePath();
	        }
	            
	    } catch (Exception e) {
	    	Log.e(LOG, "Error in loadBitmapCoverFromStorage: " + e.getMessage());
		}

	    return coverPath;
	}
	
	
	/**
	 * The directory with all the temporally images is first emptied and then deleted
	 * @param directory
	 * @return
	 */
	public boolean deleteBookCoverDirectory(String directoryName) {
		ContextWrapper cw = new ContextWrapper(appContext);
        File bitmapDirectory = cw.getDir(directoryName, Context.MODE_PRIVATE);
        
        return deleteDir(bitmapDirectory);
	}
	
	
	// This method empties and delete the directory 
	private boolean deleteDir(File directory) {
	    if (directory != null && directory.isDirectory()) {
	    	
	        String[] children = directory.list();
	        
	        for (int i = 0; i < children.length; i++) {
	            boolean success = deleteDir(new File(directory, children[i]));
	            if (!success) {
	                return false;
	            }
	        }
	    }

	    return directory.delete();
	}

}
